﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using System.Xml.Linq;
using System.IO;

namespace SharpPopulatorLib 
{

    public class Utils
    {

        #region Public Methods

        public static XDocument XMLDocumentLoader()
        {
            //To obtain providers form the DBProviders file

            XDocument doc = null;
            string localFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string fullPath = String.Format(@"{0}\{1}", localFolder, ConfigurationManager.AppSettings["DBProviders"].ToString());

            try
            {
                if (File.Exists(fullPath))
                { doc = XDocument.Load(fullPath); }
                else
                {
                    DirectoryInfo d = new DirectoryInfo(localFolder).Parent;
                    if (d.GetFiles(ConfigurationManager.AppSettings["DBProviders"]).Length == 1)
                    { doc = XDocument.Load(d.GetFiles(ConfigurationManager.AppSettings["DBProviders"])[0].FullName); }
                    else
                    { doc = XDocument.Load(d.Parent.GetFiles(ConfigurationManager.AppSettings["DBProviders"])[0].FullName); }
                }
            }
            catch (Exception e)
            { throw new Exception(e.Message); }
            return doc;
        }

        #endregion

    }

}
